# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd

def create_countclick2():
    print('读入数据  train')
    fi_train=pd.read_csv('data/origin/train.csv')      
    print('读入数据  ad')
    fi_ad=pd.read_csv('data/origin/ad.csv')
    print('读入完成')

    fi_train=pd.merge(fi_train,fi_ad,how='left',on='creativeID')

    del fi_ad 
    
    #获取所有的userID
    userID_list=fi_train['userID'].values
    userID_list=list(set(userID_list))
    
    dict_cre={}
    dict_pos={}
    dict_time={}

    #初始化字典
    for user in userID_list:
        dict_cre[user]=''
        dict_pos[user]=''
        dict_time[user]=''

    print('已统计每个用户的app,开始统计各种安装的app列表')
    
    list_user=fi_train['userID'].values
    list_time=fi_train['clickTime'].values
    list_pos=fi_train['positionID'].values
    list_cre=fi_train['creativeID'].values
    
    del fi_train
    
    total=len(list_time)
    for i in range(total):
        dict_pos[list_user[i]]+=(str(list_pos[i])+' ')
        dict_time[list_user[i]]+=(str(list_time[i])+' ')
        dict_cre[list_user[i]]+=(str(list_cre[i])+' ')
    
        if i%100000==0:
            print('字典已加载: %.2f %%'%(i/total*100))

    
    #下面的代码待定,用于保存。
    userID_list=list(dict_pos.keys())
    
    cl_pos=list(dict_pos.values())
    cl_time=list(dict_time.values())
    cl_cre=list(dict_cre.values())
    
    df_list=pd.DataFrame({'userID':userID_list,
                          'cl_time':cl_time,
                          'cl_pos':cl_pos,
                          'cl_cre':cl_cre}) 
    
    df_list.to_csv('data/feature/train_click_detail_v2.csv',index=False) 
        
if __name__=='__main__':
    create_countclick2()
